package com.qfedu.controller;

import com.alibaba.fastjson.JSONObject;
import com.qfedu.entity.Singer;
import com.qfedu.entity.SongList;
import com.qfedu.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author zhang
 * @Title: commonValidateAdd
 * @date : 2022/2/22 20:50
 */
@Controller
public class SingerController {
    @Autowired
    private SingerService singerService;

    @RequestMapping("/singer")
    @ResponseBody
    public List<Singer> findAllSinger(){
        return singerService.findAllSinger();
    }

    @RequestMapping("/singer/add")
    @ResponseBody
    public JSONObject addSinger(Singer singer) {

        int i = singerService.addSinger(singer);
        JSONObject jsonObject = new JSONObject();
        if (i > 0){
            jsonObject.put("code",1);
            jsonObject.put("msg","提交成功");
        }else {
            jsonObject.put("code",0);
            jsonObject.put("msg","提交失败");
        }
        return jsonObject;

    }

    @RequestMapping("/singer/delete")
    @ResponseBody
    public int deleteSinger(Singer singer){
        return singerService.deleteSinger(singer);
    }

    @RequestMapping("/singer/update")
    @ResponseBody
    public JSONObject updateSinger(Singer singer) {
        int i = singerService.updateSinger(singer);
        JSONObject jsonObject = new JSONObject();
        if (i > 0){
            jsonObject.put("code",1);
            jsonObject.put("msg","修改成功");
        }else {
            jsonObject.put("code",0);
            jsonObject.put("msg","修改失败");
        }
        return jsonObject;

    }

    @RequestMapping("/singer/avatar/update")
    @ResponseBody
    public JSONObject updateSingerById(MultipartFile file, Singer singer) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String url = "/img/singerPic/" + originalFilename;//originalFilename 文件名
        originalFilename = "D:\\qianfeng\\software\\IDEA\\project3-background\\project3background\\img\\singerPic\\" + originalFilename;
        System.out.println("originalFilename");

        //文件上传
        File file1 = new File(originalFilename);
        //将接收到的文件转移到新的文件下
        file.transferTo(file1);


        singer.setPic(url);
        JSONObject jsonObject = singerService.updateSingerById(singer);
        jsonObject.put("pic",url);
        return jsonObject;

    }
}
